﻿using MyWEbProject.DAO;
using MyWEbProject.DAO.Entities;
using MyWEbProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyWEbProject.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        public ActionResult Career()
        {
            ViewBag.Message = "Your career page.";

            return View();
        }
        public ActionResult mypage()
        {
            ViewBag.Message = "My page.";

            return View();
        }
        public ActionResult NewPage()
        {
            ViewBag.Message = "New page.";

            return View();
        }
        public ActionResult VizrtPage()
        {
            ViewBag.Message = "New page.";

            return View();
        }
        //public ActionResult CustomerView()
        //{
        //    ViewBag.Message = "Customer View";


        //    List<Customers> model = new List<Customers>();
        //    model.Add(new Customers { Name = "Nursat", ID = 1 });
        //    model.Add(new Customers { Name = "Madhobi", ID = 2 });

        //    //throw new Exception("Testing Server Error");
        //    ViewData["MY.CUSTOMER.LIST"] = model;
            

        //    return View();
        //}

        

        
        public String GetServerTime(String format)
        {
            String currentTime = DateTime.Now.ToString(format);//("dd MMMM yyyy hh:mm:ss tt");           
            return currentTime;
        }

        [HttpPost]
        public String GetServerTime()
        {
            //string format = "dd MMMM, yyyy hh:mm::ss"; 
            //String currentTime = DateTime.Now.ToString(format);//("dd MMMM yyyy hh:mm:ss tt");           
            //return currentTime;

            ProfessionDAO professionDAO = new ProfessionDAO();
            Profession profession = professionDAO.GetByID(1);

            List<Profession> professions = professionDAO.GetAll();
            

            Profession profession2 = new Profession();
            profession2.Name = "Marketing Executive";
            int count = professionDAO.Save(profession2);
            profession2 = new Profession();
            profession2.Name = "Doctor";
            count = professionDAO.Save(profession2);
            int CountS = professionDAO.GetCount();

            return CountS.ToString();
        }

        public ActionResult CustomerView()
        {
            CustomerDAO customerDAO = new CustomerDAO();
            List<Customer> customers = customerDAO.GetAllCustomer();
            
            
            ViewBag.ModelDetails = customers;
            if (customers == null)
            {
                return HttpNotFound();
            }
            return View(customers);
        }


        
        [HttpGet]
        public ActionResult EditCustomer(int id)
        {
            CustomerDAO customerDAO = new CustomerDAO();
            Customer customer = new Customer();
            customer = customerDAO.GetByID(id);


            ProfessionDAO professionDAO = new ProfessionDAO();
            List<Profession> professions = professionDAO.GetAll();

            if (customer == null)
            {
                return HttpNotFound();
            }

            if (id == 0)
            {
                customer.Name = "";
                customer.Address = "";
                customer.Age = 0;
                customer.Profession = 0;
                Profession p = new Profession() { ID = 0, Name = "" };
                customer.Profession1 = p;
                customer.Created_on = DateTime.Now;
            }
            ViewBag.ModelDetails = customer;



            ViewBag.ProfessionDetails = professions;


            return View(customer);
        }

        [HttpPost] // [HttpGet]
        public ActionResult EditCustomer(int id, string name ) 
        {
            CustomerDAO customerDAO = new CustomerDAO();
            Customer customer = new Customer();

            customer.ID = id;


            customer.Name = "dd";
            customer.Address = "aa";
            customer.Age = 40;
            customer.Profession = 1;
            Profession p = new Profession() { ID = 1, Name = "MBBS Doctor" };
            customer.Profession1 = p;
            customer.Created_on = DateTime.Now;

            customerDAO.SaveCustomer(customer);

            ViewBag.ModelDetails = customer;


            ProfessionDAO professionDAO = new ProfessionDAO();
            List<Profession> professions = professionDAO.GetAll();
            ViewBag.ProfessionDetails = professions;


            return View();
        }

        public ActionResult Edit(int id)
        {

            
            int customerID = Convert.ToInt32(id);
            CustomerDAO dao = new CustomerDAO();
            Customer customer = null;
            if (customerID > 0)//Update Operation
            {
                customer = dao.GetByID(customerID);
            }
            else//Insert Operation
                customer = new Customer();


            return View(customer);
        }


        [HttpPost]
        public ActionResult Edit(Customer model)
        {
            CustomerDAO dao = new CustomerDAO();
            Customer entity = null;
            if (model.ID > 0)
                entity = dao.GetByID(model.ID);
            else
            {
                entity = new Customer();
                entity.Created_on = DateTime.Now;
            }
            entity.Age = model.Age;
            entity.Address = model.Address;
            entity.Name = model.Name;
            entity.Profession = model.Profession;




            ProfessionDAO pr = new ProfessionDAO();
            Profession p = pr.GetByID(model.Profession);
            entity.Profession1 = p;           
            //List<Profession> professions =pr.GetAll();
            //entity.ProfessionList = new SelectList(professions, "ID", "Name");


            //SelectProfessionModel mo = new SelectProfessionModel();
            //mo.ID= 1;
            //mo.Professions = pr.GetAll();



            dao.SaveCustomer(entity);

            return RedirectToAction("CustomerView");
        }


        
        public String DeleteCommand(String i)
        {
            String q = Request["C_ID"];

            int id = int.Parse(q);
            

            CustomerDAO dao = new CustomerDAO();
            dao.DeleteCustomer(id);

            return "successful: " + id.ToString();
        }



        public String GetAllCustomers()
        {
            CustomerDAO dao = new CustomerDAO();
            List<Customer> customers = dao.GetAllCustomer();
            
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            //Customer customer = dao.GetByID(4);
            String json = serializer.Serialize(customers);
            //Customer customer = (Customer)serializer.Deserialize("{ID: 1, Name:'Nursat'}", typeof(Customer));
            //String json = serializer.Serialize(customer);
            return json;
        }

        //public ActionResult Save(int id)
        //{
        //    CustomerDAO customerDAO = new CustomerDAO();
        //    Customer customer = new Customer();

        //    customer.ID = id;


        //    customer.Name = "dd";
        //    customer.Address = "aa";
        //    customer.Age = 40;
        //    customer.Profession = 1;
        //    Profession p = new Profession() { ID = 1, Name = "MBBS Doctor" };
        //    customer.Profession1 = p;
        //    customer.Created_on = DateTime.Now;

        //    //customerDAO.SaveCustomer(customer);

        //    return View();
        //}
    }
}
